<template>
  <router-view></router-view>
</template>

<script setup lang="ts">

import {User} from "@/models/contacts.ts";
import {useChatStore} from "@/stores/chat.ts";
import {useUserStore} from "@/stores/user.ts";
import LoginApi from '@/api/login_mock.js';
import {getTokenAtStorage} from '@/utils/tokens.js'
import {ChatService} from "@/models/chat_service.ts";

const chatStore = useChatStore();
const userStore = useUserStore();


window.chatService = new ChatService();

let loggedIn = getTokenAtStorage() !== null;
(async function() {
  if (loggedIn) {
    chatStore.user = new User(
        userStore.userInfo.uid,
        userStore.userInfo.username,
        userStore.userInfo.nickname,
        userStore.userInfo.avatarUrl,
    )

    await chatStore.refreshChatGroups()
    await chatStore.refreshContacts();
  }
})();

console.debug(">>> App 挂载了... ")

window.user.onVerifyToken(() => {
  console.debug("onVerifyToken ...")
  //如果有token，则去验证，如果没有则路由到登录页
  //去调用验证token的接口
  if (loggedIn) {
    console.debug("to verify token ...")
    LoginApi.verifyAuth().then(resp => {
      console.log("resp = ",resp)
    })
  }
})

</script>

<style scoped>
</style>
